home *** CD-ROM | disk | FTP | other *** search
- Path: SPOD2.dev.esoc.esa.de!328PT
- From: 328pt@SPOD2.dev.esoc.esa.de (Phil Tregoning)
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: 28 Jan 1996 15:59:36 GMT
- Organization: European Space Operations Center
- Message-ID: <4eg6h8$1db5@info.estec.esa.nl>
- References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <4e6hse$dvl@ns.RezoNet.NET> <310a2389.49571776@nntp.ix.netcom.com>
- Reply-To: 328pt@SPOD2.dev.esoc.esa.de
- NNTP-Posting-Host: spod2.dev.esoc.esa.de
-
- >> In referenced article, John R Buchan says...
- >>
- >> >> cpy = (char *) malloc(MAXLEN);
- >> >
- >> >The cast is unnecessary and can hide errors. You should remove it.
- >
- >ray@ultimate-tech.com (Ray Dunn) wrote:
- >
- >> [...] adding a cast to a pointer of the same
- >> type to the malloc return is the *safest* thing you can do:
- >>
- >> fred = malloc(n * sizeof(int));
- >>
- >> Oops - fred isn't an "int *" it's a "long *", but the compiler wont
- >> issue any warnings, but in:
- >>
- >> fred = (int *)malloc(n * sizeof(int));
- >>
- >> the compiler will issue an error.
-
- miker3@ix.netcom.com (Mike Rubenstein) replied :
-
- >Why will the compiler issue an error in that case? It's completely
- >legal code.
-
- From K&R2 section 5.4 :
-
- "It is not legal to [stuff about illegal pointer arithmatic], or even,
- except for void *, to assign a pointer of one type to a pointer of
- another type without a cast."
-
- If you assign a int * to a long * the compiler can certainly pick that up.
-
- >The error that can be hidden by a cast is omitting the declaration of
- >malloc() and not including stdlib.h. Without the cast a diagnostic is
- >required since an int cannot be automatically converted to a pointer.
- >With the cast the compiler will convert the int it thinks is returned
- >by malloc() to a pointer and few compilers will give any indication of
- >the error (read: none that I'm aware of).
-
- I've also frequently seen the advice not to cast the return from malloc()
- as it supposedly can hide errors, and have followed this advice. However,
- having read what Ray Dunn wrote I thought I would try it out on my compiler
- (DEC C - with default warnings) to see which cases get picked up.
-
- Missing out #include <stdlib.h>, and with a cast on malloc() gives a
- warning and information message as follows :
-
- long_ptr = (int *)malloc(sizeof(int));
- ..................^
- %CC-I-IMPLICITFUNC, In this statement, the identifier "malloc" is implicitly
- declared as a function.
-
- long_ptr = (int *)malloc(sizeof(int));
- ^
- %CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value
- "(int ...)malloc(...)" is "int", which is not compatible with "long".
-
- I would call that fair warning that something is amiss.
-
- Including #include <stdlib.h>, and without a cast on malloc() gives no
- messages at all on "long_ptr = malloc(sizeof(int));" because a void pointer
- (returned from malloc()) is being converted to a long pointer, which is
- OK as far as the compiler is concerned.
-
- Given the results of this, I think a may start casting the return from
- malloc().
-
- Phil Tregoning
-